<?php
defined('IN_XLP') or exit('Access Denied!');

class wmOrderModel extends Model {

	function __construct() {
		parent::__construct();
		$this->dbTable = 'wm_order';
		$this->page = 20;
	}


	/**
	 * 新增数据
	 * @param   $data array
	 * @return  $result int 
	 */
	public function add($data){
		if(!is_array($data)) return false;

		$data['time_create'] = time();
		$data['order_state'] = 1;	//已下单
		
		$result = $this->insert($data);
		return $result;
	}

	/**
	 * 标志位删除
	 * @param   $id int 表主键ID
	 * @return  $result int 
	 */
	public function del($id){
		$data   = '`is_del` = 1';
		$where  = '`id` = '. $id;
		$result = $this->update($data, $where);
		return $result;
	}

	/**
	 * 获取列表数据
	 * @param   $page int 分页数据，默认是第一页
	 * @return  $result array 返回查询数组
	 */
	public function get_list($page = 1, $where = '1 = 1'){
		$result	= $this->page($page, $this->page)->where($where)->order('id desc')->findAll();
		return $result;
	}

	/**
	 * 获取指定ID的数据信息
	 * @param   $id int 表主键ID
	 * @return  $result array
	 */
	public function info($id, $field='*'){
		$result	= $this->field($field)->where('`id` = '. $id)->find();
		return $result;
	}

	public function getOrderByOutSn($out_sn, $field='*') {
		$result = $this->field($field)->where('`order_sn` = '.$out_sn)->find();
		return $result;
	}


	/*
	 * 调用微信发货接口
	 */
	public function wxDelivery($order_id, $deliver_msg="ok") {
		//订单信息
		$order_info = $this->info($order_id, '`order_sn`,`openid`,`transid`');
		//发送通知
		$result = D('weixin')->delivery($order_info['openid'], $order_info['order_sn'], $order_info['transid'], $deliver_msg);
		return $result;
	}


	/**
	 * 发货
	 * @param  int    $order_id             订单ID
	 * @param  int 	  $shipping_express_id 快递公司ID
	 * @param  string $shipping_code       物流单号
	 */
	public function deliver($order_id, $shipping_express_id, $shipping_code) {
		$data = array(
			'shipping_code' 		=> $shipping_code,
			'shipping_express_id'	=> $shipping_express_id,
			'order_state'			=> 40,	//发货状态
		);

		//通知微信发货成功 也可以发送ok之外的信息 如缺货不发等
		$wx_result = $this->wxDelivery($order_id, 'ok');
		$wx_result = json_decode($wx_result, true);
		
		if($wx_result['errcode'] == 0) {
			//更新订单状态
			M('wm_order')->update($data, array('id' => $order_id));

			return true;
		} else {
			return false;
		}
	}


	/**
	 * 取消订单
	 * @param  int 	  $order_id 订单ID
	 * @param  string $note     取消原因备注
	 */
	public function cancel($order_id, $note, $admin_id) {
		//修改订单状态
		$result = M('wm_order')->update(array('order_state'=>0),array('id'=>$order_id));
		return $result ? true : false;
	}

	/**
	 * 确认收货
	 * @param  int 	  $order_id 订单ID
	 * @param  string $note     备注
	 * @param  string $operator 操作人
	 */
	public function received($order_id, $note, $operator) {
		//修改订单状态
		$result = M('wm_order')->update(
			array('order_state'=>50, 'finish_time'=>time()),
			array('id'=>$order_id)
		);
		//日志
		$this->log($order_id, 50, $note, $operator);
		return $result;
	}

	
}